package com.atguigu.day05;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.HashSet;
import java.util.Iterator;

public class Flink04_TimeWindow_TumblingWindow_Process {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.将数据转为WaterSensor
        SingleOutputStreamOperator<WaterSensor> waterSensorStream = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //4.将相同id的数据聚合到一块
        KeyedStream<WaterSensor, String> keyedStream = waterSensorStream.keyBy(new KeySelector<WaterSensor, String>() {
            @Override
            public String getKey(WaterSensor value) throws Exception {
                return "key";
            }
        });

        //5.开启一个基于处理时间的滚动窗口，窗口大小为5s
        WindowedStream<WaterSensor, String, TimeWindow> window = keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(5)));

        //统计UV
        SingleOutputStreamOperator<String> result = window.process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
            /**
             * @param tuple    key
             * @param context  上下文对象可以获取到 窗口相关的信息 以及 当前处理时间 事件时间 WaterMark  侧输出 窗口状态
             * @param elements 迭代器，放的是窗口中的元素
             * @param out      收集器
             * @throws Exception
             */
            @Override
            public void process(String tuple, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                System.out.println("process....");

                //1.创建HashSet然后利用hashSet幂等性做去重
                HashSet<String> hashSet = new HashSet<>();

                //2. 获取到迭代器中每条数据的id放入hashSet自动去重
                for (WaterSensor element : elements) {
                    hashSet.add(element.getId());
                }


//                System.out.println(context.window().getStart() / 1000 + ":" + hashSet.size() + context.window().getEnd() / 1000);
                out.collect(context.window().getStart() / 1000 + ":" + hashSet.size() +":"+ context.window().getEnd() / 1000);
            }
        });

        result.print();

        env.execute();
    }
}
